package com.example.demo.service.impl;

import com.example.demo.entity.Coalmeasuringinstrument;
import com.example.demo.handle.MeasuringInstrumentListParseHandler;
import com.example.demo.mapper.CoalmeasuringinstrumentMapper;
import com.example.demo.service.CoalmeasuringinstrumentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-27
 */
@Service
public class CoalmeasuringinstrumentServiceImpl extends ServiceImpl<CoalmeasuringinstrumentMapper, Coalmeasuringinstrument> implements CoalmeasuringinstrumentService {

    @Autowired
    private CoalmeasuringinstrumentMapper coalmeasuringinstrumentMapper;

    @Override
    public Integer insertInstrumentData(String instrumentXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            MeasuringInstrumentListParseHandler measuringInstrumentListParseHandler = new MeasuringInstrumentListParseHandler();
            try {
                // 读取xml文件
                parser.parse(instrumentXml, measuringInstrumentListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + measuringInstrumentListParseHandler.getCoalmeasuringinstrumentArrayList().size() + "个节点被存储");
            for (Coalmeasuringinstrument coalmeasuringinstrument : measuringInstrumentListParseHandler.getCoalmeasuringinstrumentArrayList()) {
                System.out.println(coalmeasuringinstrument.getCsMineCode());
                System.out.println(coalmeasuringinstrument.getCsDataTime());
                System.out.println(coalmeasuringinstrument.getCsWellCode());
                System.out.println(coalmeasuringinstrument.getOmWeighterCode());
                System.out.println(coalmeasuringinstrument.getOmWeighterLocation());
                System.out.println(coalmeasuringinstrument.getOmOromoteMode());
                System.out.println(coalmeasuringinstrument.getOmWeighterManum());
                System.out.println(coalmeasuringinstrument.getOmWeighterMaeffec());
                System.out.println(coalmeasuringinstrument.getOmCertificateNum());
                System.out.println(coalmeasuringinstrument.getOmCertificateEffec());
                System.out.println(coalmeasuringinstrument.getOmGisnodeCode());
                System.out.println(coalmeasuringinstrument.getOmLiftWeight());
                System.out.println(coalmeasuringinstrument.getOmMcarWeight());
                System.out.println(coalmeasuringinstrument.getOmRcarWeight());
                System.out.println("----finish----");
                coalmeasuringinstrumentMapper.insertInstrumentData(coalmeasuringinstrument.getCsMineCode(),coalmeasuringinstrument.getCsDataTime(),coalmeasuringinstrument.getCsWellCode(),coalmeasuringinstrument.getOmWeighterCode(),coalmeasuringinstrument.getOmWeighterLocation(),coalmeasuringinstrument.getOmOromoteMode(),coalmeasuringinstrument.getOmWeighterManum(),coalmeasuringinstrument.getOmWeighterMaeffec(),coalmeasuringinstrument.getOmCertificateNum(),coalmeasuringinstrument.getOmCertificateEffec(),coalmeasuringinstrument.getOmGisnodeCode(),coalmeasuringinstrument.getOmLiftWeight(),coalmeasuringinstrument.getOmMcarWeight(),coalmeasuringinstrument.getOmRcarWeight());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
